package com.kadapatha.web.user;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

import com.kadapatha.web.base.BaseForm;
import com.kadapatha.web.user.validation.FieldMatch;

@FieldMatch(first = "password", second = "repassword", message = "The password fields must match.")
public class UserForm extends BaseForm {
	private Long id;
	@Size(min = 6, max = 20, message = "User name between 6 to 20 Characters.")
	@NotBlank(message = "Please enter a user name")
	private String userName;
	@Size(min = 1, max = 30, message = "First name between 1 to 10 Characters.")
	@NotBlank(message = "Please enter a first name")
	private String firstName;
	private String middleName;
	private String lastName;
	@Email(message = "Please provide a valid email address")
	@NotBlank(message = "Please enter a email address")
	private String email;
	private String rePassword;
	@Size(min = 6, max = 10, message = "Password must between 6 to 20 Characters.")
	@NotBlank(message = "Password must not be blank.")
	private String password;
	@NotBlank(message = "Please re-enter the password.")
	private String repassword;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public void addValidationMessages(
			Set<ConstraintViolation<UserForm>> failures) {
		validationErrorMap = new HashMap<String, String>();
		for (ConstraintViolation<UserForm> failure : failures) {
			validationErrorMap.put(StringUtils.isEmpty(failure
					.getPropertyPath().toString()) ? "password" : failure
					.getPropertyPath().toString(), failure.getMessage());
		}
		setError(!failures.isEmpty());
	}

	@Target({ ElementType.METHOD, ElementType.FIELD })
	@Retention(RetentionPolicy.RUNTIME)
	public @interface Password {

		String property();

	}
}
